package com.kai.exception;


import com.kai.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice //全局异常处理器
public class GlobalExcepetionHandler {

    @ExceptionHandler
    public Result defaultErrorHandler(Exception e) {

        log.error("全局异常捕获", e);
        return Result.error("服务器异常");
    }

    @ExceptionHandler
    public Result duplicateException(DuplicateKeyException e) {

        log.error("全局异常捕获", e);

        String[] split = e.getMessage().substring(e.getMessage().indexOf("Duplicate entry")).split(" ");
        String error = split[2];
        return Result.error(error + "已存在");
    }


}
